package com.group5.onlinegame.handler;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group5.onlinegame.entity.Announcement;
import com.group5.onlinegame.entity.AnnouncementType;
import com.group5.onlinegame.entity.LoginUser;
import com.group5.onlinegame.service.IAnnouncementService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("/")
public class AnnouncementHandler {

    @Resource
    private IAnnouncementService announcementService;

    @RequestMapping("getAnnouncement")
    @RequiresRoles("system")
    public String getAnnouncement(String pageNum,Model model){
        int cpage = 1;
        if (pageNum != null) {
            cpage = Integer.parseInt(pageNum);
        }
        PageHelper.startPage(cpage,5);
        List<Announcement> announcementList = announcementService.getAnnouncement();
        PageInfo<Announcement> announcementPageInfo = new PageInfo(announcementList);
        model.addAttribute("announcementPageInfo",announcementPageInfo);
        return "announcement";
    }

    @RequestMapping("delAnnouncement")
    @RequiresRoles("system")
    public void delAnnouncement(Integer id, PrintWriter out){
        Integer is = announcementService.delAnnouncement(id);
        System.out.println(is);
        out.println(is);
    }

    @RequestMapping("editAnnouncement")
    @RequiresRoles("system")
    public String goToeditAnnouncement(Model model) {
        List<AnnouncementType> announcementTypeList = announcementService.getAnnouncementType();
        model.addAttribute("announcementTypeList",announcementTypeList);
        return "announcementedit";
    }

    @RequestMapping("submitCreateAnnouncement")
    @RequiresRoles("system")
    public String submitCreateAnnouncement(String typeid, String title, String content, HttpSession session) {
        Announcement announcement = new Announcement();
        announcement.setTitle(title);
        announcement.setContent(content);
        announcement.setTypeid(Integer.parseInt(typeid));
        LoginUser loginUser = (LoginUser) session.getAttribute("loginUser");
        announcement.setUserid(loginUser.getId());
        Integer is = announcementService.createNewAnnouncement(announcement);
        return "redirect:/getAnnouncement";
    }

    @RequestMapping("updateAnnouncement")
    @RequiresRoles("system")
    public String updateAnnouncement(String id,Model model) {
        Announcement announcement = announcementService.getOneAnnouncement(Integer.parseInt(id));
        model.addAttribute("announcement",announcement);
        List<AnnouncementType> announcementTypeList = announcementService.getAnnouncementType();
        model.addAttribute("announcementTypeList",announcementTypeList);
        return "announcementedit";
    }

    @RequestMapping("doupdateAnnouncement")
    @RequiresRoles("system")
    public String doupdateAnnouncement(String id,String typeid, String title, String content) {
        Announcement announcement = new Announcement();
        announcement.setId(Integer.parseInt(id));
        announcement.setTypeid(Integer.parseInt(typeid));
        announcement.setTitle(title);
        announcement.setContent(content);
        announcementService.updateAnnouncement(announcement);
        return "redirect:/getAnnouncement";
    }
}
